﻿using Accountant.DataModel;
using FluentAssertions;
using NUnit.Framework;

namespace UnitTests
{
  [TestFixture]
  public class CrossContextOperations
  {
    [SetUp]
    public void SetUp()
    {
      _ctx = new AccountantContext();
      _bound = _ctx.Accounts.Add(new Account { Name = "Bound" });
      _unbound = new Account { Name = "Unbound" };
    }

    private AccountantContext _ctx;
    private Account _bound;
    private Account _unbound;

    [Test]
    public void SetBaseAccountBoundToUnbound()
    {
      _unbound.BaseAccount = _bound;
      _unbound.BaseAccount.Should().BeSameAs(_bound);
      _bound.DerivedAccounts.Should().BeEmpty();
    }
    [Test]
    public void SetBaseAccountUnboundToBound()
    {
      _bound.BaseAccount = _unbound;
      _bound.BaseAccount.Should().BeSameAs(_unbound);
      _bound.DerivedAccounts.Should().BeEmpty();
      _unbound.DerivedAccounts.Should().Equal(_bound);
    }

    [Test]
    public void AddDerivedAccountBoundToUnbound()
    {
      _unbound.DerivedAccounts.Add(_bound);
      _unbound.DerivedAccounts.Should().Equal(_bound);
      _bound.BaseAccount.Should().BeNull();
    }
    [Test]
    public void AddDerivedAccountUnboundToBound()
    {
      _bound.DerivedAccounts.Add(_unbound);
      _bound.DerivedAccounts.Should().Equal(_unbound);
      _unbound.BaseAccount.Should().Be(_bound);
    }
  }
}